package com.software.car.controller;

import com.software.car.api.ProjectSercvice;
import com.software.car.common.utils.CommonPage;
import com.software.car.common.utils.CommonResult;
import com.software.car.entity.dto.ProjectDto;
import com.software.car.entity.po.UserPo;
import com.software.car.entity.vo.ProjectDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 魔法はまだ解けない
 * @Date 2021/3/20
 */
@RestController
@RequestMapping("/project")
public class ProjectController {

    @Autowired
    ProjectSercvice projectSercvice;

    /**
     * 添加项目
     *
     * @return
     */
    @PostMapping("")
    @PreAuthorize("hasAnyRole('ROLE_ADMINISTER','ROLE_SUPER_ADMINISTER')")
    public CommonResult addProject(@RequestBody ProjectDto projectDto, @RequestAttribute("user")UserPo userPo) {
        projectDto.setCreateUser(userPo.getId());

        String i = projectSercvice.addProject(projectDto);
        return CommonResult.success(i);
    }

    /**
     * 删除项目
     *
     * @return
     */
    @DeleteMapping("")
    @PreAuthorize("hasAnyRole('ROLE_ADMINISTER','ROLE_SUPER_ADMINISTER')")
    public CommonResult deleteProject(@RequestParam String projectId) {
        int i = projectSercvice.deleteProject(projectId);
        if(i==0){
            return CommonResult.success("id不存在，未删除数据");
        }
        return CommonResult.success(i);
    }

    /**
     * 修改项目基本信息
     *
     * @return
     */
    @PutMapping("")
    @PreAuthorize("hasAnyRole('ROLE_ADMINISTER','ROLE_SUPER_ADMINISTER')")
    public CommonResult updateProject(@RequestBody ProjectDto projectDto, @RequestAttribute("user")UserPo userPo) {
        projectDto.setUpdateUser(userPo.getId());
        int i = projectSercvice.updateProject(projectDto);
        if(i==0){
            return CommonResult.success("id不存在，修改失败");
        }
        return CommonResult.success();
    }

    /**
     * 获取项目详情
     *
     * @return
     */
    @GetMapping("")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMINISTER','ROLE_SUPER_ADMINISTER')")
    public CommonResult getProject(@RequestParam String projectId) {
        ProjectDetailVo project = projectSercvice.getProject(projectId);
        return CommonResult.success(project);
    }

    /**
     * 获取项目简易表
     *
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMINISTER','ROLE_SUPER_ADMINISTER')")
    public CommonResult getProjectList(@RequestParam(required = false) Integer page,
                                       @RequestParam(required = false) Integer pageSize,
                                       @RequestParam(required = false) Integer state,
                                       @RequestParam(required = false) String createUser) {
        CommonPage simpleProject = projectSercvice.getSimpleProject(page, pageSize, state, createUser);
        return CommonResult.success(simpleProject);
    }
}
